package com.example.administrator.u17.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.administrator.u17.R;
import com.example.administrator.u17.adapter.UpdateAdapter;
import com.example.administrator.u17.config.NetConfig;
import com.example.administrator.u17.model.UpdateComics;
import com.example.administrator.u17.model.UpdateRootBean;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class UpdateActivity extends AppCompatActivity {

    private PullToRefreshListView listView;
    private ImageView ivBack;
    private List<UpdateComics> list;
    private UpdateAdapter adapter;
    private int pages=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_update);
        initView();
        initData();
        setData();
        setListener();
    }

    private void setData() {
        listView.setAdapter(adapter);
    }

    private void initData() {
        list=new ArrayList<>();
        adapter=new UpdateAdapter(this,list);
        loadData(pages);
    }

    private void setListener() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        listView.setMode(PullToRefreshBase.Mode.BOTH);
        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                loadData(1);
                pages=1;
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                loadData(pages);
            }
        });
    }

    private void initView() {
        listView=(PullToRefreshListView)findViewById(R.id.listview_activity_update);
        ivBack=(ImageView)findViewById(R.id.iv_activity_update_back);
    }
    private void loadData(int page){
        RequestParams params=new RequestParams(NetConfig.UPDATE_PATH+page);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                Gson g=new Gson();
                UpdateRootBean bean=g.fromJson(s,UpdateRootBean.class);
                if (pages==1){
                    list.clear();
                }
                pages++;
                list.addAll(bean.data.returnData.comics);
                listView.onRefreshComplete();
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
